22 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
23 #define D(x) cout << #x " is " << x << endl
25 const int MAXE
= 1000; //Max number of edges
26 const int oo
= INT_MAX
/ 2 - 1;
34 Builds a directed edge (u, v) with capacity c.
35 Note that actually two edges are added, the edge
36 and its complementary edge for the backflow.
38 int add_edge(int u
, int v
, int c
){
39 adj
[current_edge
] = v
;
40 cap
[current_edge
] = c
;
41 next
[current_edge
] = first
[u
];
42 first
[u
] = current_edge
++;
44 adj
[current_edge
] = u
;
45 cap
[current_edge
] = 0;
46 next
[current_edge
] = first
[v
];
47 first
[v
] = current_edge
++;
50 void initialize_max_flow(){
52 memset(next
, -1, sizeof next
);
53 memset(first
, -1, sizeof first
);
58 int arrived_by
[MAXE
]; //arrived_by[i] = The last edge used to reach node i
59 int find_augmenting_path(int src
, int snk
){
61 Make a BFS to find an augmenting path from the source to the sink.
62 Then pump flow through this path, and return the amount that was pumped.
64 memset(arrived_by
, -1, sizeof arrived_by
);
69 while (h
< t
&& arrived_by
[snk
] == -1){ //BFS
71 for (int e
= first
[u
]; e
!= -1; e
= next
[e
]){
73 if (arrived_by
[v
] == -1 && cap
[e
] > 0){
75 incr
[v
] = min(incr
[u
], cap
[e
]);
81 if (arrived_by
[snk
] == -1) return 0;
86 //Remove capacity from the edge used to reach node "cur"
87 //Add capacity to the backedge
88 cap
[arrived_by
[cur
]] -= neck
;
89 cap
[arrived_by
[cur
] ^ 1] += neck
;
90 cur
= adj
[arrived_by
[cur
] ^ 1]; //move backwards in the path
95 int max_flow(int src
, int snk
){
97 while ((neck
= find_augmenting_path(src
, snk
)) != 0){
106 while (scanf("%d", &n
)==1 && n
){
107 initialize_max_flow();
109 scanf("%d %d %d", &src
, &snk
, &e
);
112 scanf("%d %d %d", &u
, &v
, &c
);
116 printf("Network %d\n", Case
++);
117 printf("The bandwidth is %d.\n\n", max_flow(src
, snk
));